package com.es.esapi.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.es.esapi.common.CommonResponse;
import com.es.esapi.domain.ClassA;
import com.es.esapi.service.EsearchService;
import com.es.esapi.service.UploadService;
import com.es.esapi.utils.pdf_solve_Main;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.UUID;

@Service("uploadService")
public class UploadServiceImpl implements UploadService {
    @Resource
    EsearchService esearchService;

    @Override
    public CommonResponse uploadFile(MultipartFile file, String title) throws Exception {
        String fileName = file.getOriginalFilename();
        String osName = System.getProperties().get("os.name").toString().toLowerCase(Locale.ROOT);
        String path="";
        if(osName.startsWith("win")){
            path="C:\\Test\\";
        }else{
            path="/mnt/test";
        }
        File saveFile=new File(path+fileName);
        if(saveFile.exists()){
            saveFile.delete();
        }else {
            saveFile.mkdirs();
        }
        file.transferTo(saveFile);
//        移植开始
        List<ClassA> tlist = pdf_solve_Main.run(saveFile.getPath());
        System.out.println("uploadfile的tlist");
        System.out.println(tlist);
//        在此添加，直接将tlist传入函数处理
        esearchService.add_location_index(title,saveFile.getPath());
        esearchService.bulk_document(title,tlist);
        return CommonResponse.createForSuccess(saveFile.getPath());
    }
}
